{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e61cdee",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 完整制作 Excel 万年历\n",
    "<img src=\"./images/日历样式展示.png?raw=true\" width=\"100%\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0a589a2",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 7.4 进一步完善（练习要求）\n",
    "\n",
    "####  参考 01.Excel操作/ 03.Excel样式处理\n",
    "\n",
    "1. 周六、周日、节假日为红色字\n",
    "\n",
    "2. 完成一整年的日历\n",
    "\n",
    "3. 日历共用单元格的效果（单元格对角线）\n",
    "\n",
    "\n",
    "```python\n",
    "# 该样式为单元格对角线\n",
    "cal_style.shared_cell_style\n",
    "```\n",
    "\n",
    "<img src=\"./images/2024_12.png?raw=true\" width=\"30%\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4986875",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.周六、周日、节假日为红色字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5732dd42",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import xlwt\n",
    "from  calendar_utils import get_calendar_list, create_style_01, CN_WEEKS\n",
    "\n",
    "\n",
    "def create_calendar_sheet(year, month, wb, cal_style):\n",
    "    \"\"\"\n",
    "    为了后面课程可以直接用了，创建了一个函数\n",
    "    参考代码：04.制作Excel日历_1 课程中的例子，\n",
    "    @param year: 年份\n",
    "    @param month: 月份\n",
    "    @param wb: Excel工作簿对象\n",
    "    @param cal_style: 日历样式\n",
    "    @return:\n",
    "    \n",
    "    \"\"\"\n",
    "    sheet = wb.add_sheet(f'{month}月')\n",
    "\n",
    "    #  lunar_list=(a, b, c)\n",
    "    #  a: 返回的日历名称。包含农历的中文名称、二十四节气名称、农历节假日名称、公历节假日名称\n",
    "    #  b: 返回的名称类型。0.农历的中文名称 1.二十四节气名称 2.农历节假日名称 3.公历节假日名称\n",
    "    #  c: 返回农历年月日。 (lunar_year, lunar_month, lunar_day)\n",
    "    solar_list, lunar_list = get_calendar_list(year=year, month=month)\n",
    "\n",
    "    # 写入周标题\n",
    "    for w in range(0, 7):\n",
    "        sheet.write(r=0, c=w, label=CN_WEEKS[w], style=cal_style.week_style)\n",
    "\n",
    "    # 写入日历数据\n",
    "    for i, r in enumerate(range(0, len(solar_list))):\n",
    "        for w in range(0, 7):\n",
    "            lunar_info = lunar_list[i][w]\n",
    "            if lunar_info == '':\n",
    "                # 如果为空，只需写入样式\n",
    "                sheet.write(r=i*2+1, c=w, label='', style=cal_style.solar_style)\n",
    "                sheet.write(r=i*2+2, c=w, label='', style=cal_style.lunar_style)\n",
    "                continue\n",
    "\n",
    "            # 农历日历名称、名称类型、农历年月日\n",
    "            lunar_name, lunar_type, lunar_date = lunar_info\n",
    "            # *** 核心代码 ***\n",
    "            # lunar_type: 0.农历的中文名称 1.二十四节气名称 2.农历节假日名称 3.公历节假日名称\n",
    "            # w = 5、6 为周六、周日\n",
    "            if lunar_type != 0 or w in [5, 6]: \n",
    "                solar_style = cal_style.solar_highlight_style\n",
    "                lunar_style = cal_style.lunar_highlight_style\n",
    "            else:\n",
    "                solar_style = cal_style.solar_style\n",
    "                lunar_style = cal_style.lunar_style\n",
    "\n",
    "            # 写入公历\n",
    "            sheet.write(r=i*2+1, c=w, label=solar_list[i][w], style=solar_style)\n",
    "            # 写入农历\n",
    "            sheet.write(r=i*2+2, c=w, label=lunar_name, style=lunar_style)\n",
    "    return sheet\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46867cdf",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 创建一个日历样式对象\n",
    "cal_style = create_style_01()\n",
    "# 创建工作簿\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 创建 2024年2月日历\n",
    "create_calendar_sheet(2024, 2, workbook, cal_style)\n",
    "# 保存工作簿\n",
    "workbook.save('./files/2024年2月日历.xls')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13f9b79d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2.完成一整年的日历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "492f65c8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for m in range(1, 13): # range(1, 13) 的左闭右开原则\n",
    "    print(m, end=\", \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87345bd1",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import xlwt\n",
    "from  calendar_utils import create_style_01, create_style_02\n",
    "\n",
    "# 创建工作簿\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 创建一个日历样式对象\n",
    "cal_style = create_style_01()\n",
    "# 循环创建每个月份的日历\n",
    "for m in range(1, 13): \n",
    "    create_calendar_sheet(2024, m, workbook, cal_style)\n",
    "# 保存工作簿\n",
    "workbook.save('./files/2024年日历_样式1.xls')\n",
    "\n",
    "# 创建工作簿\n",
    "workbook = xlwt.Workbook(encoding='utf-8')\n",
    "# 创建一个日历样式对象2 ，大家也可以定义更多的样式 create_style_03 create_style_04 ....\n",
    "cal_style = create_style_02()\n",
    "# 循环创建每个月份的日历\n",
    "for m in range(1, 13): \n",
    "    create_calendar_sheet(2024, m, workbook, cal_style)\n",
    "# 保存工作簿\n",
    "workbook.save('./files/2024年日历_样式2.xls')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef6d372",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 3. 日历共用单元格的效果（单元格对角线）\n",
    "\n",
    "<img src=\"./images/2024_12.png?raw=true\" width=\"30%\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c37b61d1",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 实现的原理：\n",
    "1. 将第6行的日期和第五行的日期合并到一个单元格\n",
    "2. 合并单元格对角线的效果，使用以下代码\n",
    "```python\n",
    "sheet.merge(..., style=cal_style.shared_cell_style)\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}